package com.hyh.baowenlioaworking.service.impl;

import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.mapper.AdminMapper;
import com.hyh.baowenlioaworking.model.Admin;
import com.hyh.baowenlioaworking.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.service.impl
 * @ClassName AdminServiceImpl
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  14:06
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Value("${default.userFace}")
    private String defaultUserFace; // 默认头像路径
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 登录方法
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public boolean login(String username, String password) {
        // 查询数据库获取对应用户名的用户信息
        System.out.println("我进到service啦");

        Admin admin = adminMapper.login(username, password);
        System.out.println("我从mapper出来啦");
        // 判断用户是否存在以及密码是否匹配
        if (admin != null) {
            // 进行密码比对
//            String encryptedPassword = encryptPassword(password);
            return password.equals(admin.getPassword());
        } else {
            return false; // 登录失败
        }
    }

    @Override
    public String addAdmin(Admin admin) {
        List<String> usernameList = adminMapper.getUsernameList();
        System.out.println(usernameList);
        System.out.println(usernameList.contains(admin.getUsername()));
        if (usernameList.contains(admin.getUsername())){
            return "用户名重复";
        }else {
            // 对密码进行加密
            String encryptedPassword = encryptPassword(admin.getPassword());
            admin.setPassword(encryptedPassword);

            // 设置默认头像
            if (admin.getUserFace() == null || admin.getUserFace().isEmpty()) {
                admin.setUserFace(defaultUserFace);
            }

            int i = adminMapper.addAdmin(admin);
            if (i!=0){
                return "注册成功";
            }else {
                return "注册失败";
            }
        }

    }

    // 对密码进行加密（使用MD5算法）
    private String encryptPassword(String password) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(password.getBytes(StandardCharsets.UTF_8));
            return DatatypeConverter.printHexBinary(digest).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Admin> getAdminByUserName(String username) {
        return adminMapper.getAdminByUserName(username);
    }
}
